<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Apollo Admin - 修改密码</title>
    
    <!-- Reset Stylesheet -->
    <link rel="stylesheet" href="/back/css/reset.css" type="text/css" media="screen" />
    <!-- Main Stylesheet -->
    <link rel="stylesheet" href="/back/css/style.css" type="text/css" media="screen" />
    
    <!-- jQuery -->
    <script type="text/javascript" src="/back/scripts/jquery-2.1.0.min.js"></script>
    
    <style>
        body#change-password {
            color: #fff;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
        }

        #change-password-wrapper {
            background: rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(10px);
            border-radius: 20px;
            padding: 40px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
            border: 1px solid rgba(255, 255, 255, 0.2);
            width: 100%;
            max-width: 450px;
        }

        #change-password-wrapper #change-password-top {
            text-align: center;
            margin-bottom: 30px;
        }

        #change-password-top h1 {
            font-size: 32px;
            font-weight: 300;
            margin-bottom: 8px;
            background: linear-gradient(45deg, #fff, rgba(255, 255, 255, 0.8));
            background-clip: text;
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }

        #change-password-top p {
            font-size: 16px;
            opacity: 0.9;
            margin: 0;
        }

        #change-password-content {
            position: relative;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            opacity: 0.9;
        }

        .form-group input {
            width: 100%;
            padding: 15px 20px;
            border: 1px solid rgba(255, 255, 255, 0.3);
            border-radius: 10px;
            background: rgba(255, 255, 255, 0.1);
            color: #fff;
            font-size: 16px;
            transition: all 0.3s ease;
            box-sizing: border-box;
        }

        .form-group input:focus {
            outline: none;
            border-color: rgba(255, 255, 255, 0.6);
            background: rgba(255, 255, 255, 0.15);
            box-shadow: 0 0 20px rgba(255, 255, 255, 0.1);
        }

        .form-group input::placeholder {
            color: rgba(255, 255, 255, 0.6);
        }

        .change-password-button {
            width: 100%;
            padding: 15px;
            background: linear-gradient(45deg, #667eea, #764ba2);
            color: #fff;
            border: none;
            border-radius: 10px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-bottom: 15px;
        }

        .change-password-button:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
        }

        .change-password-button:active {
            transform: translateY(0);
        }

        .change-password-button:disabled {
            opacity: 0.6;
            cursor: not-allowed;
            transform: none;
        }

        .back-button {
            width: 100%;
            padding: 12px;
            background: rgba(255, 255, 255, 0.1);
            color: #fff;
            border: 1px solid rgba(255, 255, 255, 0.3);
            border-radius: 10px;
            font-size: 14px;
            cursor: pointer;
            transition: all 0.3s ease;
            text-decoration: none;
            display: inline-block;
            text-align: center;
        }

        .back-button:hover {
            background: rgba(255, 255, 255, 0.15);
            border-color: rgba(255, 255, 255, 0.5);
        }

        .notification {
            padding: 12px 20px;
            border-radius: 8px;
            margin-bottom: 20px;
            font-weight: 500;
            display: none;
            position: relative;
        }

        .notification.success {
            background: linear-gradient(45deg, #4CAF50, #45a049);
            color: #fff;
        }

        .notification.error {
            background: linear-gradient(45deg, #f44336, #da190b);
            color: #fff;
        }

        .loading {
            text-align: center;
            margin: 15px 0;
            display: none;
            color: rgba(255, 255, 255, 0.8);
            font-size: 14px;
        }

        .loading::after {
            content: '';
            display: inline-block;
            width: 16px;
            height: 16px;
            border: 2px solid rgba(255, 255, 255, 0.3);
            border-top: 2px solid #fff;
            border-radius: 50%;
            margin-left: 10px;
            animation: spin 1s linear infinite;
        }

        @keyframes spin {
            from { transform: rotate(0deg); }
            to { transform: rotate(360deg); }
        }

        .password-tips {
            background: rgba(255, 255, 255, 0.1);
            border-radius: 8px;
            padding: 15px;
            margin-bottom: 20px;
            font-size: 13px;
            line-height: 1.5;
        }

        .password-tips h4 {
            margin: 0 0 10px 0;
            font-size: 14px;
            color: #fff;
        }

        .password-tips ul {
            margin: 0;
            padding-left: 20px;
            color: rgba(255, 255, 255, 0.8);
        }

        .password-tips li {
            margin-bottom: 5px;
        }
    </style>
</head>
<body id="change-password">
    <div id="change-password-wrapper">
        <div id="change-password-top">
            <h1>Apollo Admin</h1>
            <p>修改密码</p>
        </div>
        
        <div id="change-password-content">
            <div id="notification" class="notification"></div>
            
            <div class="password-tips">
                <h4>密码安全要求：</h4>
                <ul>
                    <li>密码长度不少于6位</li>
                    <li>建议包含字母、数字和特殊字符</li>
                    <li>新密码不能与当前密码相同</li>
                </ul>
            </div>
            
            <form id="changePasswordForm">
                <div class="form-group">
                    <label for="oldPassword">当前密码</label>
                    <input type="password" id="oldPassword" name="oldPassword" placeholder="请输入当前密码" required>
                </div>
                
                <div class="form-group">
                    <label for="newPassword">新密码</label>
                    <input type="password" id="newPassword" name="newPassword" placeholder="请输入新密码（至少6位）" required>
                </div>
                
                <div class="form-group">
                    <label for="confirmPassword">确认新密码</label>
                    <input type="password" id="confirmPassword" name="confirmPassword" placeholder="请再次输入新密码" required>
                </div>
                
                <button type="submit" id="changePasswordButton" class="change-password-button">
                    修改密码
                </button>
                
                <div id="loading" class="loading">
                    正在修改密码...
                </div>
            </form>
        </div>
    </div>

    <script>
        $(document).ready(function() {
            // 表单提交事件
            $('#changePasswordForm').on('submit', function(e) {
                e.preventDefault();
                handleChangePassword();
            });
            
            // 实时验证密码确认
            $('#confirmPassword').on('input', function() {
                const newPassword = $('#newPassword').val();
                const confirmPassword = $(this).val();
                
                if (confirmPassword && newPassword !== confirmPassword) {
                    $(this).css('border-color', 'rgba(244, 67, 54, 0.6)');
                } else if (confirmPassword && newPassword === confirmPassword) {
                    $(this).css('border-color', 'rgba(76, 175, 80, 0.6)');
                } else {
                    $(this).css('border-color', 'rgba(255, 255, 255, 0.3)');
                }
            });
            
            // 密码长度验证
            $('#newPassword').on('input', function() {
                const password = $(this).val();
                if (password.length > 0 && password.length < 6) {
                    $(this).css('border-color', 'rgba(255, 152, 0, 0.6)');
                } else if (password.length >= 6) {
                    $(this).css('border-color', 'rgba(76, 175, 80, 0.6)');
                } else {
                    $(this).css('border-color', 'rgba(255, 255, 255, 0.3)');
                }
            });
        });

        function handleChangePassword() {
            const oldPassword = $('#oldPassword').val().trim();
            const newPassword = $('#newPassword').val().trim();
            const confirmPassword = $('#confirmPassword').val().trim();

            // 前端验证
            if (!oldPassword || !newPassword || !confirmPassword) {
                showNotification('请填写完整的密码信息', 'error');
                return;
            }

            if (newPassword.length < 6) {
                showNotification('新密码长度不能少于6位', 'error');
                return;
            }

            if (newPassword !== confirmPassword) {
                showNotification('两次输入的新密码不一致', 'error');
                return;
            }

            if (oldPassword === newPassword) {
                showNotification('新密码不能与当前密码相同', 'error');
                return;
            }

            setLoading(true);

            $.ajax({
                url: '/api/admin/changePassword',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify({
                    oldPassword: oldPassword,
                    newPassword: newPassword,
                    confirmPassword: confirmPassword
                }),
                success: function(response) {
                    if (response.code === 200) {
                        showNotification('密码修改成功！', 'success');
                    } else {
                        showNotification(response.message || '密码修改失败', 'error');
                    }
                },
                error: function(xhr, status, error) {
                    console.error('修改密码请求失败:', error);
                    if (xhr.status === 401) {
                        showNotification('登录已过期，请重新登录', 'error');
                        setTimeout(function() {
                            window.location.href = '/admin/login';
                        }, 2000);
                    } else {
                        showNotification('网络错误，请重试', 'error');
                    }
                },
                complete: function() {
                    setLoading(false);
                }
            });
        }

        function showNotification(message, type) {
            const notification = $('#notification');
            notification.removeClass('error success')
                       .addClass(type)
                       .text(message)
                       .show();
            
            if (type === 'error') {
                setTimeout(function() {
                    notification.fadeOut();
                }, 5000);
            }
        }

        function setLoading(loading) {
            const button = $('#changePasswordButton');
            const loadingDiv = $('#loading');
            
            if (loading) {
                button.prop('disabled', true).text('修改中...');
                loadingDiv.show();
            } else {
                button.prop('disabled', false).text('修改密码');
                loadingDiv.hide();
            }
        }
    </script>
</body>
</html>